package com.maila.simplerecord.view;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.maila.simplerecord.R;
import com.maila.simplerecord.data.ProjectProgressdata;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/6/6.
 */

public class EditProgressPop extends PopupWindow {

    private View mMenuView;
    private Calendar cal;


    public EditProgressPop(final Activity context, final ProjectProgressdata data) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_edit_progress, null);
        final ViewHolder vh = new ViewHolder(mMenuView);
        TestSet(vh.edContent,data.subject_content);
        TestSet(vh.edNote,data.subject_note);
        TestSet(vh.tvTime,data.subject_time);
        vh.tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.subject_note=vh.edNote.getText().toString();
                data.subject_time=vh.tvTime.getText().toString();
                data.subject_content=vh.edContent.getText().toString();
                data.save();
                EventBus.getDefault().post("progress");
                dismiss();
            }
        });
        vh.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProjectProgressdata.deleteByID(data.subid);
                EventBus.getDefault().post("progress");
                dismiss();
            }
        });
        vh.tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimeDialog("请设置进度时间",context,data,vh.tvTime);
            }
        });
        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.fadepopupanimation);
        ColorDrawable dw = new ColorDrawable(80000000);
        this.setBackgroundDrawable(dw);

    }

    private void TestSet(TextView clintPhone, String client_phone) {
        if (client_phone != null) {
            clintPhone.setText(client_phone);
        }

    }
    private void TestSet(EditText clintPhone, String client_phone) {
        if (client_phone != null) {
            clintPhone.setText(client_phone);
        }
    }

    void showTimeDialog(String hint, Activity activity, final ProjectProgressdata data, final TextView tv) {

        cal = Calendar.getInstance();
        String sDate = "";
        sDate=data.subject_time;
        if (sDate!=null&&!sDate.trim().isEmpty()) {
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                Date date1 = format.parse(sDate);
                cal.setTime(date1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        //获取年月日时分秒的信息
        int year = cal.get(Calendar.YEAR);
        //month从0开始计算(一月month = 0)
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        //设置标题
        activity.setTitle(year + "-" + month + "-" + day + " " + hour + ":" + minute);
        //日历选择器的对话框形式
        new CustomDatePickerDialog(activity, R.style.Theme_Alert_Dialog, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                        setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                tv.setText(year + "." + (monthOfYear + 1) + "." + dayOfMonth);
                data.subject_time=year + "." + (monthOfYear + 1) + "." + dayOfMonth;
            }
        }, year, cal.get(Calendar.MONTH), day, hint).show();

    }

    static class ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ed_content)
        EditText edContent;
        @BindView(R.id.ed_note)
        EditText edNote;
        @BindView(R.id.tv_back)
        TextView tvDelete;
        @BindView(R.id.tv_ok)
        TextView tvOk;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}